package com.nine.news.modular.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.nine.news.constants.RedisKey;
import com.nine.news.entity.ServerLog;
import com.nine.news.enums.ServerType;
import com.nine.news.enums.SmsType;
import com.nine.news.mapper.ServerLogMapper;
import com.nine.news.modular.user.param.UserLogonCodeParam;
import com.nine.news.modular.user.service.SmsService;
import com.nine.news.response.R;
import com.nine.news.util.PhoneUtil;
import com.nine.news.util.SmsUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author: szhan
 * @Date: 2023/03/21/9:00 PM
 * @Description:
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Resource
    private ServerLogMapper logMapper;
    @Resource
    private StringRedisTemplate template;


    @Override
    public R sendRcode(String phone) {
        return sendCode(phone, SmsUtil.REGISTER, RedisKey.AUTH_LOGON_CODE, SmsType.注册验证码);
    }

    @Override
    public R checkRcode(UserLogonCodeParam param) {
        if (param != null && PhoneUtil.checkPhone(param.getPhone())) {
            if (template.hasKey(RedisKey.AUTH_LOGON_CODE + param.getPhone())) {
                Integer code = Integer.parseInt(template.opsForValue().get(RedisKey.AUTH_LOGON_CODE + param.getPhone()));
                if (code.equals(param.getCode())) {
                    template.delete(RedisKey.AUTH_LOGON_CODE + param.getPhone());
                    template.opsForSet().add(RedisKey.AUTH_ADD_PHONE,param.getPhone());
                    return R.OK();
                } return R.ERROR("验证码不正确");
            } return R.ERROR("验证码不存在或已过期");
        } return R.ERROR("不能为空");
    }

    @Override
    public R all() {
        return R.OK(logMapper.selectList(null));
    }

    @Override
    public R sendLcode(String phone) {
        return sendCode(phone, SmsUtil.LOGIN, RedisKey.AUTH_LOGIN_CODE,SmsType.登录验证码);
    }

    @Override
    public R sendFcode(String phone) {
        return sendCode(phone, SmsUtil.FIND, RedisKey.AUTH_FIND_CODE, SmsType.密码找回验证码);
    }

    @Override
    public R checkFcode(UserLogonCodeParam param) {
        if (param != null) {
            if (template.hasKey(RedisKey.AUTH_FIND_CODE + param.getPhone())) {
                if (template.opsForValue().get(RedisKey.AUTH_FIND_CODE + param.getPhone()).equals(param.getCode())) {
                    template.delete(RedisKey.AUTH_FIND_CODE + param.getPhone());
                    template.opsForSet().add(RedisKey.AUTH_FINDPHONE, param.getPhone());
                    return R.OK();
                } return R.ERROR("验证码不正确");
            } return R.ERROR("验证码不存在或已过期");
        } return R.ERROR("不能为空");
    }

    /**
     * 发送短信验证码
     * @param phone
     * @param tem 短信模版 使用SmsUtil中的参数
     * @param key Redis中的key，使用RedisKey中的常量
     * @param smsType 短信类型  直接使用SmsType中的枚举
     * @return
     */
    private R sendCode(String phone, String tem, String key, SmsType smsType){
        //1.校验
        if (PhoneUtil.checkPhone(phone)) {
            //2.生成验证码
            Integer code = RandomUtil.randomInt(100000, 999999);
            //3.发送短信 并校验是否成功
            //boolean flag = SmsUtil.sendMsg(tem, phone, code);
            if (true) {
                //4.发送成功,记录验证码
                //有效期  Redis String（每个验证码发送时间不一定一样）key 包含手机号 有效期 10分钟
                template.opsForValue().set(key + phone, code.toString(),10, TimeUnit.MINUTES);
                //5.记录发送日志
                logMapper.insert(new ServerLog(phone, ServerType.短信服务.getValue(), smsType.toString(), ServerType.短信服务.getPrice()));
                return R.OK();
            } return R.ERROR("验证码发送失败");
        } return R.ERROR("手机号格式有问题");





        //返回结果
    }
}
